/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
io.js
From http://gratisdei.com/io.js
*/

/////////////////////////////////////////////////
/////////////////////////////////////////////////
//
// Basic JavaScript File and Directory IO module
// By: MonkeeSage, v0.1
//
/////////////////////////////////////////////////
/////////////////////////////////////////////////

AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  

	if (typeof(JSIO) != 'boolean') {
	
		var JSIO = true;
	
		/////////////////////////////////////////////////
		// Basic file IO object based on Mozilla source 
		// code post at forums.mozillazine.org
		/////////////////////////////////////////////////
	
		// Example use:
		// var fileIn = FileIO.open('/test.txt');
		// if (fileIn.exists()) {
		// 	var fileOut = FileIO.open('/copy of test.txt');
		// 	var str = FileIO.read(fileIn);
		// 	var rv = FileIO.write(fileOut, str);
		// 	rv = FileIO.write(fileOut, str, 'a');
		// 	rv = FileIO.unlink(fileOut);
		// }
	
		AE.FileIO = {
	
			localfileCID  : '@mozilla.org/file/local;1',
			localfileIID  : Components.interfaces.nsILocalFile,
	
			finstreamCID  : '@mozilla.org/network/file-input-stream;1',
			finstreamIID  : Components.interfaces.nsIFileInputStream,
	
			foutstreamCID : '@mozilla.org/network/file-output-stream;1',
			foutstreamIID : Components.interfaces.nsIFileOutputStream,
	
			sinstreamCID  : '@mozilla.org/scriptableinputstream;1',
			sinstreamIID  : Components.interfaces.nsIScriptableInputStream,
	
			suniconvCID   : '@mozilla.org/intl/scriptableunicodeconverter',
			suniconvIID   : Components.interfaces.nsIScriptableUnicodeConverter,
	
			open   : function(path) {
				try {
					var file = Components.classes[this.localfileCID]
									.createInstance(this.localfileIID);
					file.initWithPath(path);
					return file;
				}
				catch(e) {
					alert(e);
					return false;
				}
			},
	
			read   : function(file, charset) {
				try {
					var data     = new String();
					var fiStream = Components.classes[this.finstreamCID]
										.createInstance(this.finstreamIID);
					var siStream = Components.classes[this.sinstreamCID]
										.createInstance(this.sinstreamIID);
					fiStream.init(file, 1, 0, false);
					siStream.init(fiStream);
					data += siStream.read(-1);
					siStream.close();
					fiStream.close();
					if (charset) {
						data = this.toUnicode(charset, data);
					}
					return data;
				} 
				catch(e) {
					return false;
				}
			},
	
			write  : function(file, data, mode, charset) {
				try {
					var foStream = Components.classes[this.foutstreamCID]
										.createInstance(this.foutstreamIID);
					if (charset) {
						data = this.fromUnicode(charset, data);
					}
					var flags = 0x02 | 0x08 | 0x20; // wronly | create | truncate
					if (mode == 'a') {
						flags = 0x02 | 0x10; // wronly | append
					}
					foStream.init(file, flags, 0664, 0);
					foStream.write(data, data.length);
					// foStream.flush();
					foStream.close();
					return true;
				}
				catch(e) {
					alert(e);
					return false;
				}
			},
	
			create : function(file) {
				try {
					file.create(0x00, 0664);
					return true;
				}
				catch(e) {
					return false;
				}
			},
	
			unlink : function(file) {
				try {
					file.remove(false);
					return true;
				}
				catch(e) {
					return false;
				}
			},
	
			path   : function(file) {
				try {
					return 'file:///' + file.path.replace(/\\/g, '\/')
								.replace(/^\s*\/?/, '').replace(/\ /g, '%20');
				}
				catch(e) {
					return false;
				}
			},
	
			toUnicode   : function(charset, data) {
				try{
					var uniConv = Components.classes[this.suniconvCID]
										.createInstance(this.suniconvIID);
					uniConv.charset = charset;
					data = uniConv.ConvertToUnicode(data);
				} 
				catch(e) {
					// foobar!
				}
				return data;
			},
	
			fromUnicode : function(charset, data) {
				try {
					var uniConv = Components.classes[this.suniconvCID]
										.createInstance(this.suniconvIID);
					uniConv.charset = charset;
					data = uniConv.ConvertFromUnicode(data);
					// data += uniConv.Finish();
				}
				catch(e) {
					// foobar!
				}
				return data;
			}
	
		}
	
	
		/////////////////////////////////////////////////
		// Basic Directory IO object based on JSLib 
		// source code found at jslib.mozdev.org
		/////////////////////////////////////////////////
	
		// ---------------------------------------------
		// ----------------- Nota Bene -----------------
		// ---------------------------------------------
		// Some possible types for get are:
		// 	'ProfD'				= profile
		// 	'DefProfRt'			= user (e.g., /root/.mozilla)
		// 	'UChrm'				= %profile%/chrome
		// 	'DefRt'				= installation
		// 	'PrfDef'				= %installation%/defaults/pref
		// 	'ProfDefNoLoc'		= %installation%/defaults/profile
		// 	'APlugns'			= %installation%/plugins
		// 	'AChrom'				= %installation%/chrome
		// 	'ComsD'				= %installation%/components
		// 	'CurProcD'			= installation (usually)
		// 	'Home'				= OS root (e.g., /root)
		// 	'TmpD'				= OS tmp (e.g., /tmp)
	
		AE.DirIO = {
	
			sep        : '/',
	
			dirservCID : '@mozilla.org/file/directory_service;1',
		
			propsIID   : Components.interfaces.nsIProperties,
		
			fileIID    : Components.interfaces.nsIFile,
	
			get    : function(type) {
				try {
					var dir = Components.classes[this.dirservCID]
									.createInstance(this.propsIID)
									.get(type, this.fileIID);
					return dir;
				}
				catch(e) {
					return false;
				}
			},
	
			open   : function(path) {
				return AE.FileIO.open(path);
			},
	
			create : function(dir) {
				try {
					dir.create(0x01, 0775);//Modification by danwatt : we want it to be x, so we can go into is
					return true;
				}
				catch(e) {
					return false;
				}
			},
	
			read   : function(dir, recursive) {
				var list = new Array();
				try {
					if (dir.isDirectory()) {
						if (recursive == null) {
							recursive = false;
						}
						var files = dir.directoryEntries;
						list = this._read(files, recursive);
					}
				}
				catch(e) {
					// foobar!
				}
				return list;
			},
	
			_read  : function(dirEntry, recursive) {
				var list = new Array();
				try {
					while (dirEntry.hasMoreElements()) {
						list.push(dirEntry.getNext()
										.QueryInterface(AE.FileIO.localfileIID));
					}
					if (recursive) {
						var list2 = new Array();
						for (var i = 0; i < list.length; ++i) {
							if (list[i].isDirectory()) {
								files = list[i].directoryEntries;
								list2 = this._read(files, recursive);
							}
						}
						for (i = 0; i < list2.length; ++i) {
							list.push(list2[i]);
						}
					}
				}
				catch(e) {
				   // foobar!
				}
				return list;
			},
	
			unlink : function(dir, recursive) {
				try {
					if (recursive == null) {
						recursive = false;
					}
					dir.remove(recursive);
					return true;
				}
				catch(e) {
					return false;
				}
			},
	
			path   : function (dir) {
				return AE.FileIO.path(dir);
			},
	
			split  : function(str, join) {
				var arr = str.split(/\/|\\/), i;
				str = new String();
				for (i = 0; i < arr.length; ++i) {
					str += arr[i] + ((i != arr.length - 1) ? 
											join : '');
				}
				return str;
			},
	
			join   : function(str, split) {
				var arr = str.split(split), i;
				str = new String();
				for (i = 0; i < arr.length; ++i) {
					str += arr[i] + ((i != arr.length - 1) ? 
											this.sep : '');
				}
				return str;
			}
		
		}
	
		if (navigator.platform.toLowerCase().indexOf('win') > -1) {
			AE.DirIO.sep = '\\';
		}
	
	}
}});